package com.zhouyer.study.utils;

/**
 * http协议工具类，主要是提供响应头信息，我们这里只提供200和404的情况
 *
 * @author zhouyao
 * @version 1.0
 * @date 2021/9/14 10:53 上午
 */
public class HttpProtocolUtil {
    /**
     * 为响应码为200的提供响应头信息
     *
     * @param contentLength 响应信息的长度
     * @return java.lang.String
     * @author zhouyao
     * @date 2021/9/14 11:00 上午
     */
    public static String getHttpHeader200(long contentLength) {
        return "HTTP/1.1 200 OK \n" +
                "Content-Type: text/html;charset=utf-8 \n" +
                "Content-Length: " + contentLength + "\n" +
                "\r\n";
    }

    /**
     * 为响应码为404的提供响应头信息(这里包含了响应信息)
     *
     * @return java.lang.String
     * @author zhouyao
     * @date 2021/9/14 11:02 上午
     */
    public static String getHttpHeader404() {
        String str404 = "<h1>404 not found</h1>";
        return "HTTP/1.1 404 NOT FOUND \n" +
                "Content-Type: text/html;charset=utf-8 \n" +
                "Content-Length: " + str404.getBytes().length + "\n" +
                "\r\n" + str404;
    }
}
